#include "stdafx.h"
#include "Pipes\PipeManager.h"

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string readFromFile(LPCWSTR fileName);

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int PipeServer(LPCWSTR pipeName, LPCWSTR path)
  {

//--- create named pipe
   CPipeManager manager;

   if(!manager.Create(pipeName)) 
      return(-1);
//--- wait for client
   char          answer[256];
   int           value=1234567890;
/*
   wprintf(L"Client: waiting for connection...\n");
   while(!manager.IsConnected())
     {
      if(!manager.ReadString(answer,_countof(answer)-1))
        {
         //Sleep(250);
         continue;
        }
      wprintf(L"Client: connected as '%S'\n",answer);
     }
*/
//--- send data to client
   wprintf(L"Server: send string\n");
   if(!manager.SendString(readFromFile(path).c_str()))
     {
      wprintf(L"Server: sending string failed\n");
      return(-1);
     }

 /*  wprintf(L"Server: sending integer\n");
   if(!manager.Send(&value,sizeof(value)))
     {
      wprintf(L"Server: sending integer failed\n");
      return(-1);
     }

   */
 //--- read data from client
   wprintf(L"Server: reading string\n");
   if(!manager.ReadString(answer,_countof(answer)-1))
     {
      wprintf(L"Server: reading string failed\n");
      return(-1);
     }
   wprintf(L"Server: '%S' received\n",answer);

   /*

   wprintf(L"Server: reading integer\n");
   if(!manager.Read(&value,sizeof(value)))
     {
      wprintf(L"Server: reading integer failed\n");
      return(-1);
     }
   wprintf(L"Server: %d received\n",value);
   */
   //--- benchmark
  /* double  volume=0.0;
   double *buffer=new double[1024*1024];   // 8 Mb

   wprintf(L"Server: start benchmark\n");
   if(buffer)
     {
      //--- fill the buffer
      for(size_t j=0;j<1024*1024;j++)
         buffer[j]=j;
      //--- send 8 Mb * 128 = 1024 Mb to client
      DWORD   ticks=GetTickCount();

      for(size_t i=0;i<128;i++)
        {
         //--- setup guard signatures
         buffer[0]=i;
         buffer[1024*1024-1]=i+1024*1024-1;
         //--- 
         if(!manager.Send(buffer,sizeof(double)*1024*1024))
           {
            wprintf(L"Server: benchmark failed, %d\n",GetLastError());
            break;
           }
         volume+=sizeof(double)*1024*1024;
         wprintf(L".");
        }
      wprintf(L"\n");
      //--- read confirmation
      if(!manager.Read(&value,sizeof(value)) || value!=12345)
         wprintf(L"Server: benchmark confirmation failed\n");
      //--- show statistics
      ticks=GetTickCount()-ticks;
      if(ticks>0)
         wprintf(L"Server: %.0lf Mb sent at %.0lf Mb per second\n",volume/1024/1024,volume/1024/ticks);
      //---
      delete[] buffer;
     }*/
//---
   return(0);
  }

//+------------------------------------------------------------------+

string readFromFile(LPCWSTR fileName) {
  string line;
  string text;
  ifstream myfile (fileName);
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      
	  text+=line+"\n";
    }
    myfile.close();
	
  }

  else cout << "Unable to open file"; 
  return text;

}

//+------------------------------------------------------------------+

int _tmain(int argc, _TCHAR* argv[])
  {
   wprintf(L"MQL5 Pipe Server (Reading.. FILE PATH and PIPE NAME)\n"); 

   if (argc<2) {
	   cout << "Wrong File Path or Pipe Name \n";
	   return(0);
   }
   LPCWSTR pipeName = argv[1];
   LPCWSTR path = argv[2];

//--- create named pipe
   while(true)
     {
      PipeServer(pipeName,path);
     }
//---
   return(0);
  }



